package com.GroupSix.controller;

import com.GroupSix.entity.StudentEntity;
import com.GroupSix.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.GroupSix.vo.StudentVo;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    //注入学生接口
    @Autowired
    private StudentService studentService;

    //增加一个学生信息
    @PostMapping("/add")
    public String add(@RequestBody StudentEntity studentEntity){
        Integer i = studentService.add(studentEntity);
        if(i>0){
            return "success";
        }
        else {
            return "fail";
        }
    }

    //注册一个学生用户
    @PostMapping("/signUp")
    public String signUp(@RequestParam("name") String name, @RequestParam("id") Integer id,
                         @RequestParam("pwd") String pwd, @RequestParam("class") String stuClass){
        return studentService.signUp(name, id, pwd, stuClass);
    }

    //根据学号删除学生信息
    @DeleteMapping("/deleteBySno/{sno}")
    public String deleteBySno(@PathVariable("sno") Integer sno){
        Integer i = studentService.delete(sno);
        if(i>0){
            return "删除成功";
        }
        else {
            return "删除失败，该学号不存在";
        }
    }

    //根据学号修改学生信息
    @PutMapping("/update")
    public String update(@RequestBody StudentEntity student){
        Integer i = studentService.update(student);
        if(i>0){
            return "修改成功";
        }
        else {
            return "修改失败";
        }
    }

    //分页查询
    @GetMapping("/list")
    public IPage<StudentEntity> list(@RequestBody StudentVo studentVo){
        StudentEntity student= new StudentEntity();
        student.setId(studentVo.getId());
        return studentService.list(student, studentVo.getCurpage(), studentVo.getLimit());
    }

    //返回所有的学生
    @GetMapping("/allStudent")
    public List<StudentEntity> getAllStudent() {
        return studentService.list();
    }

    //根据学号查询学生
    @GetMapping("/findByStudentId/{studentId}")
    public StudentEntity findBySno(@PathVariable("studentId") Integer studentId) {
        return studentService.getByStudentId(studentId);
    }

    //根据姓名查询学生信息
    @GetMapping("/findByStudentName/{studentName}")
    public StudentEntity findByStudentName(@PathVariable("studentName") String studentName) {
        return studentService.getByStudentName(studentName);
    }
}
